package com.warehouse.service;

import com.warehouse.dao.AdminDao;
import com.warehouse.dao.impl.AdminDaoImpl;
import com.warehouse.entity.Admin;
import com.warehouse.utils.Page;

import java.util.List;

public class AdminService {

    private AdminDao adminDao = new AdminDaoImpl();

    //登录
    public Admin Login(String phone, String password) {
        return adminDao.Login(phone, password);
    }

    public Page<Admin> queryPage(int start, int pageSize) {
        if (start < 1) {
            start = 1;
        }
        int pageCount = adminDao.count();//总记录数
        int totalPage = pageCount % pageSize == 0 ? (pageCount / pageSize) : (pageCount / pageSize) + 1;//总页数
        if (start >= totalPage) {
            start = totalPage;
        }
        int startPage = (start - 1) * pageSize;//计算当前页数
        List<Admin> list = adminDao.allPage(startPage, pageSize);
        return new Page<Admin>(start, totalPage, pageSize, list);
    }

    public int add(Admin admin) {
        return adminDao.add(admin);
    }

    public int update(Admin admin) {
        return adminDao.update(admin);
    }


    public int delete(int id) {
        return adminDao.delete(id);
    }

    public Admin byId(int id) {
        return adminDao.byId(id);
    }

    public Page<Admin> byCardNum(int start, int pageSize, String AdminNum) {
        if (start < 1) {
            start = 1;
        }
        int pageCount = adminDao.countByAdminNum(AdminNum);//总记录数
        int totalPage = pageCount % pageSize == 0 ? (pageCount / pageSize) : (pageCount / pageSize) + 1;//总页数
        if (start >= totalPage) {
            start = totalPage;
        }
        int startPage = (start - 1) * pageSize;//计算当前页数
        List<Admin> list = adminDao.byAdminNum(startPage, pageSize, AdminNum);
        return new Page<Admin>(start, totalPage, pageSize, list);
    }

}
